const FN = require('../../utils/public');
import util from '../../utils/util.js';
import integralAPI from '../../api/integralAPI.js'
var videoAd = null;
var app = getApp();
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integralObj: {},
    pointsConfig4watchVideo: [],
    adUnitId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page = 1;
    var that = this;
    that.initData();
    this.createAD();
  },

  // 在页面onLoad回调事件中创建激励视频广告实例 
  createAD() {
    var that = this, adUnitId = "";
    var pageUrl = util.getCurrentPageUrl()
    var item = app.globalData.advertisement.find(obj => obj.advertisementPage === pageUrl);
    if (item && item.unitId && wx.createRewardedVideoAd) {
      adUnitId = item.unitId
      that.setData({
        adUnitId
      })
      videoAd = wx.createRewardedVideoAd({
        adUnitId: adUnitId
      })
      videoAd.onLoad(() => {
        console.log('激励视频 广告加载成功')
      })
      videoAd.onError((err) => {
        console.log("onError")
        console.log(err)
      })
      videoAd.onClose((res) => {
        // 用户点击了【关闭广告】按钮
        if (res && res.isEnded) {
          console.log('正常播放结束，可以下发游戏奖励')// 正常播放结束，可以下发游戏奖励
          that.surplusIntegral();
        } else {
          console.log('播放中途退出，不下发游戏奖励')
          // 播放中途退出，不下发游戏奖励
        }
      })
    }
  },

  initData: function () {
    var that = this;
    const item4watchVideo = app.globalData.miniappInfo.pointsconfigList.find(item => item.pointsConfigKey == 'watchVideo');
    this.setData({
      pointsConfig4watchVideo: item4watchVideo
    })

    integralAPI.getIntegralDetail({
      pageNum: page,
      pageSize: 6
    }).then(res => {
      that.setData({
        integralObj: res.data
      })
    })
  },

  // 激励视频观看完后，添加4个积分 
  surplusIntegral() {
    var that = this;
    var pointsConfig4watchVideo = that.data.pointsConfig4watchVideo;
    var integralDetailNum = 0;
    if (pointsConfig4watchVideo) {
      integralDetailNum = pointsConfig4watchVideo.pointsConfigType === "1" ?
        pointsConfig4watchVideo.pointsConfigValue :
        -pointsConfig4watchVideo.pointsConfigValue;
      var integraldetail = {
        integralId: app.globalData.userInfo.integralObj.integralId,
        integralDetailType: pointsConfig4watchVideo ? pointsConfig4watchVideo.pointsConfigType : null,
        integralDetailDesc: pointsConfig4watchVideo ? pointsConfig4watchVideo.pointsConfigName : null,
        integralDetailNum: integralDetailNum
      };

      integralAPI.saveInteralByadVideo({
        integralId: app.globalData.userInfo.integralObj.integralId,
        integraldetail
      }).then(res => {
        if (res.data > 0) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }
      })
    }
  },

  getMoreDetail: function () {
    page++;
    var that = this;
    integralAPI.getMoreIntegralDetail({
      pageNum: page,
      pageSize: 6
    }).then(res => {
      var resArr = that.data.integralObj.jfdetail.list;
      if (res.data.list.length == 0) {
        FN.Toast('没有更多数据了')
      } else {
        var moreData = "integralObj.jfdetail.list";
        resArr.push.apply(resArr, res.data.list)
        that.setData({
          [moreData]: resArr
        })
      }
    })
  },

  // 购买积分
  buyIntegral: function () {
    var that = this;
    wx.showLoading({
      title: '跳转中···',
    })
    wx.navigateToMiniProgram({
      appId: 'wxbf6140c14ee1b85c',
      path: 'pages/buyIntegral/buyIntegral?correctOpenId=' + that.data.integralObj.jfobj.wx.openId + "&integralId=" + that.data.integralObj.jfobj.integralId,
      extraData: {

      },
      envVersion: 'release',// develop	开发版	trial	体验版	release	正式版	teacherName = Ewen&wdTeacherId=3405616241f788fee0bd3caa346b3798&rewardId=fee2ee672a9d2ca04919279441d8f163&correctRecordBatch=923a31e2032b447ab01ed42daeee807f
      success(res) {
        // 打开成功
        console.log("打开成功");
        console.log(res);
      }, fail: function () {
        console.log("打开成功");
      }, complete: function () {
        wx.hideLoading();
      }
    })
  },

  // 获取积分
  goIndex: function () {
    // 在合适的位置打开广告
    if (videoAd) {
      videoAd.show().catch(() => {
        videoAd.load().then(() => videoAd.show()).catch(err => {
          FN.Toast('激励视频 广告显示失败')
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.onAppShow((res) => {
      if (res.referrerInfo.extraData) {
        // 代表支付成功
        if (res.referrerInfo.extraData.wxpayres.errMsg === "requestPayment:ok") {
          FN.Toast('购买成功')
          that.initData();
        } else {
          FN.Toast('购买失败')
        }
      }
    })
  },

})
